<template>
    <div class="quill-editor">
        <a-upload  :showUploadList="false"
                   :action="uploadAction"  @change="handleChange">
            <div style="display: none;">
                <div id="editor-upload">上传</div>
            </div>
        </a-upload>
        <quill-editor ref="myQuillEditor" v-model="editorContent" :options="editorOption"
                      @change="onEditorChange($event)"> </quill-editor>
    </div>

</template>

<script>
    import { quillEditor, Quill } from 'vue-quill-editor'
    import ImageResize from 'quill-image-resize-module'
    Quill.register('modules/imageResize', ImageResize);
    export default {
        data(){
            return{
                uploadAction:this.UPLOAD_URL,
                editorContent:'',
                editorOption:{
                    modules:{
                        toolbar:{
                            container:[
                                ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                                ['blockquote', 'code-block'],
                                [{ 'header': 1 }, { 'header': 2 }],        // 标题，键值对的形式；1、2表示字体大小
                                [{ 'list': 'ordered'}, { 'list': 'bullet' }],     //列表
                                [{ 'script': 'sub'}, { 'script': 'super' }],   // 上下标
                                [{ 'indent': '-1'}, { 'indent': '+1' }],     // 缩进
                                [{ 'direction': 'rtl' }],             // 文本方向
                                [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
                                [{ 'header': [1, 2, 3, 4, 5, 6, false] }],     //几级标题
                                [{ 'color': [] }, { 'background': [] }],     // 字体颜色，字体背景颜色
                                [{ 'font': [] }],     //字体
                                [{ 'align': [] }],    //对齐方式
                                ['clean'],    //清除字体样式
                                ['image']    //上传图片
                            ],
                            handlers: {
                                'image': function (value) {
                                    if (value) {
                                        // console.log("===重写")
                                        document.querySelector('#editor-upload').click()
                                    } else {
                                        this.quill.format('image', false);
                                    }
                                }
                            }
                        },
                        imageResize: {
                            displayStyles:{
                                backgroundColor: 'black',
                                border: 'none',
                                color: 'white'
                            },
                            modules: [ 'Resize', 'DisplaySize'],
                        }
                    }
                }
            }
        },
        components: {quillEditor},
        props:{
            content:{
                type:String,
                default:''
            }
        },
        watch:{
            content:function (val,oldVal) {
                this.editorContent = val;
            }
        },
        methods:{
            onEditorChange({ editor, html, text }){
                this.$emit('update:content',html);
                this.$emit('result',html);
            },
            handleChange(info) {
                this.loading = true
                if(info.file.status == 'uploading') {
                    const isJPG = info.file.type === 'image/jpeg';
                    const isPNG = info.file.type === 'image/png';
                    const isGif = info.file.type === 'image/gif';
                    if (!isJPG && !isPNG && !isGif) {
                        this.$message.error('图片格式错误,请检查后重新上传!');
                        this.loading = false;
                        return;
                    }
                    const isLt500KB = info.file.size / 1024 / 500
                    if (!isLt500KB) {
                        this.$message.error('图片大小不合适,请上传小于500KB的图片!');
                        this.loading = false;
                        return;
                    }
                }
                if (info.file.status === 'done') {
                    let quill = this.$refs.myQuillEditor.quill;
                    // 获取光标所在位置
                    let length = quill.getSelection().index;
                    // 插入图片
                    quill.insertEmbed(length, 'image',info.file.response.data)
                    // 调整光标到最后
                    quill.setSelection(length + 1)
                } else if (info.file.status === 'error') {
                    this.$message.error(`${info.file.name} file upload failed.`);
                }
            },
        },
    }
</script>

<style>
    .ql-container{
        height: 300px;
    }
</style>